<?php
namespace Api\Controller;
use Think\Controller;
class JiancaiController extends CommonController {
    public function anli(){
        $map = array();
        if($_GET['openid']){
            $eq['openid'] = I("get.openid");
            $map['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        }else{
            $map['shop_id'] = $_GET['shop_id'];
        }
        $list = M("JiancaiAnli")->where($map)->order("last_time desc")->select();
        foreach ($list as $key => $value) {
            $list[$key]['pics'] = explode(',', $value['pics']);
        }
        $this->ajaxReturn($list);
    }

    public function anliAdd(){
        $data = $_POST;
        foreach ($data as $key => $value) {
            if(!is_numeric($value) && empty($value)){
                $arr = array("err_code"=>400,"err_msg"=>"请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }

        $eq['openid'] = I("get.openid");
        $data['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        if(M("JiancaiAnli")->add($data)){
            $arr = array("err_code"=>0,"err_msg"=>"添加成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙！");
            $this->ajaxReturn($arr);
        }
    }

    public function goodsForm(){
        if($_GET['id']){
            $map['wid'] = session('wid');
            $map['type'] = 'jiancai';
            $map['id'] = $_GET['id'];
            $info = M("Goods")->where($map)->find();
            $pics = explode(',', $info['pics']);
            $res['add_url'] = "https://www.58zltc.com/api.php/Goods/add/id/{$_GET['id']}";
        }else{
            $res['add_url'] = "https://www.58zltc.com/api.php/Goods/add";
        }
        $form[] = array('type'=>'text', 'title'=>'商品名称', 'name'=>'title', 'value'=>$info['title']?$info['title']:'');
        $form[] = array('type'=>'text', 'title'=>'单位', 'name'=>'unit', 'value'=>$info['unit']?$info['unit']:'');
        $form[] = array('type'=>'digit', 'title'=>'优惠价', 'name'=>'price', 'value'=>$info['price']?$info['price']:'');
        $form[] = array('type'=>'top');
        $form[] = array('type'=>'pic', 'title'=>'商品图片', 'name'=>'pics', 'num'=>2, 'pics'=>$pics?$pics:array());
        $form[] = array('type'=>'top');
        $form[] = array('type'=>'textarea', 'title'=>'商品简介', 'name'=>'content', 'value'=>$info['content']?$info['content']:'');
        $res['form'] = $form;
        $this->ajaxReturn($res);
    }

    public function goodsAdd(){

    }

    public function addOrder(){
        $map['wid'] = session('wid');
        $map['id'] = I("goods_id");
        $info = M("Goods")->where($map)->find();
        if(!$info){
            $arr = array("err_code"=>400,"err_msg"=>"非法操作！");
            $this->ajaxReturn($arr);
        }
        $new['wid'] = $info['wid'];
        $new['shop_id'] = $info['shop_id'];
        $new['goods_id'] = I("goods_id");
        $new['openid'] = I("openid");
        $new['tel'] = I("tel");
        $new['title'] = $info['title'];
        $new['img_url'] = $info['img_url'];
        $new['price'] = $info['price'];
        $new['unit'] = $info['unit'];
        $new['content'] = $info['content'];

        $new['status'] = 0;
        $new['create_time'] = date("Y-m-d H:i:s");
        if(M("YuyueLog")->add($new)){
            $arr = array("err_code"=>0,"err_msg"=>"预约成功！");
            $this->ajaxReturn($arr);
        }else{
            // $arr = array("err_code"=>400,"err_msg"=>"网络忙！".M("YuyueLog")->getLastSql());
            $arr = array("err_code"=>400,"err_msg"=>"网络忙！");
            $this->ajaxReturn($arr);
        }
    }

    public function yuyue(){
        $map['wid'] = session('wid');
        $map['openid'] = I("openid");

        if(I('id')){
            $map['id'] = I('id');
            $list = M("YuyueLog")->where($map)->find();
        }else{
            $list = M("YuyueLog")->where($map)->select();
        }
        $this->ajaxReturn($list);
    }
}